package com.atguigu.common.result;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Alice
 * @since 2021-08-27 星期五 16:50
 */
@Data
public class R {
  private Integer code;
  private String message;
  private Map<String, Object> data = new HashMap<>();

  private R() {
  }

  private void setResponseStatus(ResponseEnum response) {
    this.setCode(response.getCode());
    this.setMessage(response.getMessage());
  }

  public static R ok() {
    R r = new R();
    r.setResponseStatus(ResponseEnum.SUCCESS);
    return r;
  }

  public static R error() {
    R r = new R();
    r.setResponseStatus(ResponseEnum.ERROR);
    return r;
  }

  public static R result(ResponseEnum response) {
    R r = new R();
    r.setResponseStatus(response);
    return r;
  }

  public R data(String key, Object value) {
    this.getData().put(key, value);
    return this;
  }

  public R data(Map<String, Object> map) {
    this.getData().putAll(map);
    return this;
  }

  public R code(Integer code) {
    this.setCode(code);
    return this;
  }

  public R message(String message) {
    this.setMessage(message);
    return this;
  }
}
